//
//  ViewController.m
//  10503
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) NSArray *arr;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *juhua;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)pressed:(id)sender
{
    [self.juhua startAnimating];
    NSMutableArray *imagearr=[NSMutableArray array];
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        dispatch_group_t group=dispatch_group_create();
        for(int i=0;i<self.arr.count;i++)
        {
            dispatch_group_async(group, queue, ^{
                UIImageView *image=[self addimage:i andX:i];
                [imagearr addObject:image];
            });

            dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
            dispatch_async(dispatch_get_main_queue(), ^{
                for(long int i=0;i<imagearr.count;i++)
                {
                    UIImageView *im=imagearr[i];
                    [self.view addSubview:im];
                }
            });
        }
        [self.juhua stopAnimating];
    });
}
- (IBAction)pressed2:(id)sender
{
    [self.juhua startAnimating];
     NSMutableArray *imagearr=[NSMutableArray array];
     dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
     dispatch_async(queue, ^{
         for(int i=0;i<self.arr.count;i++)
         {
             UIImageView *image=[self addimage:i andX:i];
             [imagearr addObject:image];
         }
         dispatch_async(dispatch_get_main_queue(), ^{
             for(long int i=0;i<imagearr.count;i++)
             {
                 UIImageView *im=imagearr[i];
                 [self.view addSubview:im];
             }
             [self.juhua stopAnimating];
         });
     });
}
-(UIImageView *)addimage:(int)imagename andX:(int)x
{
    int y=100;
    NSString *urlStr = self.arr[imagename];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    if(x<3)
    {
        x-=0;
        y=100;
    }
    else if(x>=3 && x<6)
    {
        x-=3;
        y=200;
    }
    else if (x>=6 &&x<9)
    {
        x-=6;
        y=300;
    }
    else
    {
        x-=9;
        y=400;
    }
    imgView.frame = CGRectMake(x*100, y, 100, 100);
    return imgView;
}
@end
